package com.wandian.news.mvvm.toutiao.toutiaoactivity;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mylib.libcore.adapter.BasePagerAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.R;
import com.wandian.news.databinding.NewsActivityToutiaoBinding;
import com.wandian.news.mvvm.toutiao.totiaopage.TouTiaoChildFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 头条activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_NEWS_TOUTIAO)
public class TouTiaoActivity extends BaseMVVMActivity<TouTiaoActivityViewModel> {
    private NewsActivityToutiaoBinding dataBinding;
    private BasePagerAdapter myAdapter;
    private List<String> titleList=new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.news_activity_toutiao);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
//        mViewModel.getTitleListData();
        initViewData();
        initClink();
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("头条赚");
        titleList.clear();
        fragments.clear();
        titleList.add("国内新闻");//guonei
        titleList.add("国际新闻");//world
        titleList.add("财经新闻");//caijing
        titleList.add("军事新闻");//military
        titleList.add("科技新闻");//keji
        titleList.add("IT资讯");//it
        titleList.add("汽车新闻");//auto
        titleList.add("娱乐新闻");//huabian
        titleList.add("体育新闻");//tiyu
        titleList.add("足球新闻");//football
        titleList.add("健康知识");//health
        initTabPage();
    }

    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> {
            finish();
        });
    }

    //初始化tabpage
    private void initTabPage() {
        for(int i=0;i<titleList.size();i++){
            fragments.add(TouTiaoChildFragment.newInstance(titleList.get(i).trim()));
        }
        myAdapter = new BasePagerAdapter(getSupportFragmentManager(), fragments,titleList);
        if(UIUtils.isEmpty(dataBinding.viewPager.getAdapter())){
            dataBinding.viewPager.setAdapter(myAdapter);
            dataBinding.viewPager.setOffscreenPageLimit(titleList.size());
            dataBinding.tabLayout.setViewPager(dataBinding.viewPager);
        }
        if(!UIUtils.isEmpty(myAdapter)){
            myAdapter.notifyDataSetChanged();
        }
        //预加载数量
        dataBinding.viewPager.setOffscreenPageLimit(1);
        dataBinding.tabLayout.setViewPager(dataBinding.viewPager);
        dataBinding.tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        dataBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }


}
